﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;

namespace YJKFamilyComparison
{
	internal class ElementTypeResultPacker : ResultPackerBase
	{
		public Autodesk.Revit.DB.ElementType ThisElementType
		{
			get
			{
				return this.thisElementType;
			}
		}

		public static ElementTypeResultPacker MakeACloneNoneValue(ElementTypeResultPacker input)
		{
			ElementTypeResultPacker elementTypeResultPacker = new ElementTypeResultPacker();
			elementTypeResultPacker.UseId = input.UseId;
			elementTypeResultPacker.UseName = input.UseName;
			elementTypeResultPacker.NodeName = "无";
			elementTypeResultPacker.thisElementType = input.ThisElementType;
			elementTypeResultPacker.parameters = new Dictionary<string, List<KeyValuePair<string, string>>>();
			foreach (KeyValuePair<string, List<KeyValuePair<string, string>>> keyValuePair in input.parameters)
			{
				elementTypeResultPacker.parameters.Add(keyValuePair.Key, new List<KeyValuePair<string, string>>());
				foreach (KeyValuePair<string, string> keyValuePair2 in keyValuePair.Value)
				{
					if (!string.IsNullOrEmpty(keyValuePair2.Value) && !string.IsNullOrWhiteSpace(keyValuePair2.Value))
					{
						elementTypeResultPacker.parameters[keyValuePair.Key].Add(new KeyValuePair<string, string>(keyValuePair2.Key, "无"));
					}
				}
			}
			return elementTypeResultPacker;
		}

		public ElementTypeResultPacker()
		{
		}

		public ElementTypeResultPacker(Autodesk.Revit.DB.ElementType elementtype)
		{
			this.UseId = elementtype.UniqueId.ToString().GetHashCode();
			this.UseName = elementtype.Name;
			this.NodeName = elementtype.Name;
			this.thisElementType = elementtype;
			this.parameters = new Dictionary<string, List<KeyValuePair<string, string>>>();
		}

		internal void Order()
		{
			int count = this.parameters.Keys.Count;
			Dictionary<string, List<KeyValuePair<string, string>>>.KeyCollection keys = this.parameters.Keys;
			for (int i = 0; i < count; i++)
			{
				string key = keys.ElementAt(i);
				this.parameters[key] = (from k in this.parameters[key]
				orderby k.Key
				select k).ToList<KeyValuePair<string, string>>();
			}
			this.parameters = (from k in this.parameters
			orderby k.Key
			select k).ToDictionary((KeyValuePair<string, List<KeyValuePair<string, string>>> t) => t.Key, (KeyValuePair<string, List<KeyValuePair<string, string>>> t) => t.Value);
		}

		internal void AddParameter(string id, KeyValuePair<string, string> input)
		{
			if (!this.parameters.Keys.Contains(id))
			{
				this.parameters.Add(id, new List<KeyValuePair<string, string>>());
			}
			this.parameters[id].Add(input);
		}

		public void MakeParameters()
		{
			foreach (object obj in this.thisElementType.Parameters)
			{
				Parameter parameter = (Parameter)obj;
				string id = parameter.Id.ToString();
				string name = parameter.Definition.Name;
				string parameterValue = Utility.GetParameterValue(parameter);
				KeyValuePair<string, string> input = new KeyValuePair<string, string>(name, parameterValue);
				this.AddParameter(id, input);
			}
		}

		public Dictionary<string, List<KeyValuePair<string, string>>> parameters;

		private Autodesk.Revit.DB.ElementType thisElementType;
	}
}
